<template>
  <div>
    <el-table :data="dishes">
      <el-table-column label="id" prop="dishId" />
      <el-table-column label="菜品名称" prop="dishName" />
      <el-table-column label="菜品图片">
        <template #default="scope">
          <img :src="scope.row.dishPic" />
        </template>
      </el-table-column>
      <el-table-column label="价格" prop="price"></el-table-column>

      <el-table-column label="操作">
        <template #default="scope">
          <el-button
            type="success"
            icon="edit"
            @click="modify(scope.row.dishId)"
            circle
          ></el-button>
          <el-button
            type="danger"
            icon="delete"
            @click="del(scope.row.dishId)"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination background @current-change="pageChange" v-model:current-page="pno" :page-size="3" layout="->,prev, pager, next" :total="totalcount" />
    <!-- 点击修改弹出一个框框 -->
    <el-dialog v-model="dialogFormVisible" title="修改分店信息">
      <el-form
        style="width: 500px"
        :model="form"
        label-width="120px"
        class="demo-ruleForm"
      >
        <el-form-item label="菜品名称">
          <el-input v-model="form.dishName"></el-input>
        </el-form-item>
        <el-form-item label="菜品图片">
          <el-upload
            class="avatar-uploader"
            name="avatar"
            action="http://localhost:9000/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img v-if="form.dishPic" :src="form.dishPic" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload>
        </el-form-item>
        <el-form-item label="菜品价格" prop="price">
          <el-input v-model="form.price"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancel" type="success">取消</el-button>
          <el-button @click="confirm" type="primary">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, watch, onMounted, toRefs } from "vue";
import { ElMessage } from "element-plus";
import $http from "@/http";
let data = reactive<any>({
  dishes: [],
});
let form = ref<any>({
  dishName: "",
  dishPic: "",
  price: 0,
  dishId: 0,
});

const handleAvatarSuccess = (res: any, file: any) => {
  //console.log(res)
  form.value.dishPic = "http://localhost:9000/" + res.filename;
};
let totalcount=ref(0)
let pno=ref(1);
let pcount=ref(3)
let { dishes } = toRefs(data);
// let dishes = reactive<any>([]);
const dialogFormVisible = ref(false);
// 取消修改
const cancel = () => {
  dialogFormVisible.value = false;
};
// 确认修改
const confirm = () => {
  $http.dishApi
    .modify(form.value)
    .then((res) => {
      //console.log(res)
      if (res.data.code == 200) {
        ElMessage.success("修改菜品信息成功");
        // 修改后，重新加载列表

        loadList();
        dialogFormVisible.value = false;
      }
    })
    .catch((err) => console.log(err));
};
//分页查询
const loadList = () => {
  $http.dishApi
    .queryByPage({pno:pno.value,pcount:pcount.value})
    .then((res) => {
      //console.log(res)
      dishes.value = res.data.data[0];
      totalcount.value=res.data.total
    })
    .catch((err) => console.log(err));
};

//分页页码改变时
const pageChange=(page:any)=>{
  //console.log(page)
  pno.value=page
  loadList()
}


onMounted(() => {
  loadList();
});

const modify = (id: number) => {
  dialogFormVisible.value = true;
  $http.dishApi
    .queryById(id)
    .then((res) => {
      //console.log(res);
      form.value = res.data.data[0];
    })
    .catch((err) => console.log(err));
};

const del = (id: number) => {
  $http.dishApi
    .del(id)
    .then((res) => {
      //console.log(res)
      if (res.data.code == 200) {
        ElMessage.success("删除菜品成功");
        //删除后，重新加载列表
        loadList();
      }
    })
    .catch((err) => console.log(err));
};
</script>

<style lang="scss">
.el-table .cell:nth-child(1) {
  width: 200px;
  height: 100px;
  > img {
    width: 100%;
    height: 100%;
  }
}
</style>

<style lang="scss" scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>